//
//  SettingLayer.cpp
//  petgame
//
//  Created by zz on 2018/12/15.
//

#include "SettingLayer.hpp"
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
//引入音乐库

//创建对象
Scene* SettingLayer::createScene(){
//    创建场景
    auto scene=Scene::create();
    auto layer=SettingLayer::create();
    scene->addChild(layer);
    return scene;
}

SettingLayer* SettingLayer::create(){
    
//    创建设置对象
    auto settinglayer=new SettingLayer();
//    初始化
    if(settinglayer&&settinglayer->init()){
//        自动释放
        settinglayer->autorelease();
        return settinglayer;
    }else{
        
//        安全释放
        CC_SAFE_DELETE(settinglayer);
        return NULL;
    }
}
bool SettingLayer::init(){
    
//父类初始化
    if(!Layer::init()){
        
        return false;
    }
//    获取屏幕尺寸
    this->visibleSize=Director::getInstance()->getVisibleSize();
//    创建背景图片
    auto back=Sprite::create("mmbg.jpg");
    back->setPosition(this->visibleSize/2);
    back->setScale(0.5);
    this->addChild(back);
    
//    场景返回按钮
    auto backbtn=MenuItemImage::create("gg4.png","gg4s.png",CC_CALLBACK_1(SettingLayer::toBack,this));
//    设置返回按钮坐标
    backbtn->setPosition(Vec2(20,this->visibleSize.height-20));
//    设置返回按钮缩放
    backbtn->setScale(0.4);
    
//    设置音乐设置ui
    auto sp1=Sprite::create("bgmusicsp.png");
    sp1->setPosition(Vec2(60,this->visibleSize.height/2+60));
    sp1->setAnchorPoint(Point(0,0.5));
    sp1->setScale(0.8);
    this->addChild(sp1);
//    设置游戏设置ui
    auto sp2=Sprite::create("gamec.png");
    sp2->setPosition(Vec2(60,this->visibleSize.height/2-40));
    sp2->setAnchorPoint(Point(0,0.5));
    sp2->setScale(0.8);
    this->addChild(sp2);

//    创建toggle按钮
    auto b1=MenuItemLabel::create(LabelTTF::create("ON", "Arial",18));
    auto b2=MenuItemLabel::create(LabelTTF::create("OFF", "Arial", 18));
    this->toggle=MenuItemToggle::createWithCallback(CC_CALLBACK_1(SettingLayer::toggleFun, this), b1,b2,NULL);
    
    this->toggle->setPosition(Point(220,this->visibleSize.height/2+50));
    this->toggle->setAnchorPoint(Point(0,0.5));
    this->toggle->setSelectedIndex(0);
    
    
    auto clean=MenuItemLabel::create(LabelTTF::create("清除","Arial", 18),CC_CALLBACK_1(SettingLayer::toDelete,this));
    clean->setPosition(Vec2(240,this->visibleSize.height/2-50));
    
    
    auto menu=Menu::create(backbtn,this->toggle,clean,NULL);
    menu->setPosition(Vec2::ZERO);
    this->addChild(menu);
    
    return true;
}


void SettingLayer::toBack(Ref*pSender){
//    创建跳转，回到主页
    auto scene=HelloWorld::createScene();
    Director::getInstance()->replaceScene(scene);
}


void SettingLayer::toggleFun(Ref*pSender){
//    检查当前toggle按钮状态
    if(this->toggle->getSelectedIndex()==0){
//        记录游戏设置的数据，这里为音乐设置
        UserDefault::getInstance()->setBoolForKey("bgmusic", true);
//        开始播放音乐
        SimpleAudioEngine::getInstance()->playBackgroundMusic("bgmusics.mp3");
    }else{
//        更新存储的游戏设置数据
        UserDefault::getInstance()->setBoolForKey("bgmusic", false);
//        停止播放游戏背景音乐
        SimpleAudioEngine::getInstance()->stopBackgroundMusic();
    }
}

void SettingLayer::toDelete(Ref*pSender){
    
    
    
    
}
